﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Yz.Base;
using Yz.Model.Dal;

namespace Yz.Core.Base
{
    public class DbAccess<TEntity> : BaseAccess<TEntity>, IDbAccess<TEntity> where TEntity : BaseEntity
    {

        public YzContext _yzContext { get; set; }

        #region 新增
        public override ReturnCode Add(TEntity model, Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return db.Add(model, where);
        }
        public override async Task<ReturnCode> AddAsync(TEntity model, Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return await db.AddAsync(model, where);
        }
        public override ReturnCode Add(TEntity model, List<Expression<Func<TEntity, bool>>> whereList)
        {
            var db = _yzContext;
            return db.Add(model, whereList);
        }
        public override async Task<ReturnCode> AddAsync(TEntity model, List<Expression<Func<TEntity, bool>>> whereList)
        {
            var db = _yzContext;
            return await db.AddAsync(model, whereList);
        }
        public override TEntity AddGet(TEntity model, Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return db.AddGet(model, where);
        }
        public override async Task<TEntity> AddGetAsync(TEntity model, Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return await db.AddGetAsync(model, where);
        }
        public override TEntity AddGet(TEntity model, List<Expression<Func<TEntity, bool>>> whereList)
        {
            var db = _yzContext;
            return db.AddGet(model, whereList);
        }
        public override async Task<TEntity> AddGetAsync(TEntity model, List<Expression<Func<TEntity, bool>>> whereList)
        {
            var db = _yzContext;
            return await db.AddGetAsync(model, whereList);
        }
        public override int BatchAdd(List<TEntity> modelList)
        {
            var db = _yzContext;
            return db.BatchAdd(modelList);
        }
        public override async Task<int> BatchAddAsync(List<TEntity> modelList)
        {
            var db = _yzContext;
            return await db.BatchAddAsync(modelList);
        }
        #endregion

        #region 修改
        public override ReturnCode Update(Expression<Func<TEntity, TEntity>> epsUpdate, Expression<Func<TEntity, bool>> where1 = null, Expression<Func<TEntity, bool>> where2 = null)
        {
            var db = _yzContext;
            return db.Update(epsUpdate, where1, where2);
        }
        public override async Task<ReturnCode> UpdateAsync(Expression<Func<TEntity, TEntity>> epsUpdate, Expression<Func<TEntity, bool>> where1 = null, Expression<Func<TEntity, bool>> where2 = null)
        {
            var db = _yzContext;
            return await db.UpdateAsync(epsUpdate, where1, where2);
        }
        public override ReturnCode Update(Expression<Func<TEntity, TEntity>> epsUpdate, List<Expression<Func<TEntity, bool>>> whereList2, Expression<Func<TEntity, bool>> where1 = null)
        {
            var db = _yzContext;
            return db.Update(epsUpdate, whereList2, where1);
        }
        public override async Task<ReturnCode> UpdateAsync(Expression<Func<TEntity, TEntity>> epsUpdate, List<Expression<Func<TEntity, bool>>> whereList2, Expression<Func<TEntity, bool>> where1 = null)
        {
            var db = _yzContext;
            return await db.UpdateAsync(epsUpdate, whereList2, where1);
        }
        public override int BatchUpdate(Expression<Func<TEntity, TEntity>> epsUpdate, List<TEntity> modelList)
        {
            var db = _yzContext;
            return db.BatchUpdate(epsUpdate, modelList);
        }
        public override async Task<int> BatchUpdateAsync(Expression<Func<TEntity, TEntity>> epsUpdate, List<TEntity> modelList)
        {
            var db = _yzContext;
            return await db.BatchUpdateAsync(epsUpdate, modelList);
        }
        public override int BatchUpdate(Expression<Func<TEntity, TEntity>> epsUpdate, Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return db.BatchUpdate(epsUpdate, where);
        }
        public override async Task<int> BatchUpdateAsync(Expression<Func<TEntity, TEntity>> epsUpdate, Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return await db.BatchUpdateAsync(epsUpdate, where);
        }
        #endregion

        #region 删除
        public override ReturnCode Delete(Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return db.Delete(where);
        }
        public override async Task<ReturnCode> DeleteAsync(Expression<Func<TEntity, bool>> where)
        {
            var db = _yzContext;
            return await db.DeleteAsync(where);
        }
        public override int BatchDelete(Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return db.BatchDelete(where);
        }
        public override async Task<int> BatchDeleteAsync(Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return await db.BatchDeleteAsync(where);
        }
        #endregion

        #region 查询  
        public override int GetTotal(Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return db.GetTotal(where);
        }
        public override async Task<int> GetTotalAsync(Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return await db.GetTotalAsync(where);
        }
        public override ReturnList<T> GetListByPage<T>(Expression<Func<TEntity, bool>> where = null, ParaPage paraPage = null, List<ParaOrder<TEntity>> orders = null, bool hasTotal = true)
        {
            var db = _yzContext;
            return db.GetListByPage<TEntity, T>(base.AutoMapperConfigProvider, where, paraPage, orders, hasTotal);
        }
        public override async Task<ReturnList<T>> GetListByPageAsync<T>(Expression<Func<TEntity, bool>> where = null, ParaPage paraPage = null, List<ParaOrder<TEntity>> orders = null, bool hasTotal = true)
        {
            var db = _yzContext;
            return await db.GetListByPageAsync<TEntity, T>(base.AutoMapperConfigProvider, where, paraPage, orders, hasTotal);
        }
        public override T GetModel<T>(Expression<Func<TEntity, bool>> where = null, List<ParaOrder<TEntity>> orders = null)
        {
            var db = _yzContext;
            return db.GetModel<TEntity, T>(base.AutoMapperConfigProvider, where, orders);
        }
        public override async Task<T> GetModelAsync<T>(Expression<Func<TEntity, bool>> where = null, List<ParaOrder<TEntity>> orders = null)
        {
            var db = _yzContext;
            return await db.GetModelAsync<TEntity, T>(base.AutoMapperConfigProvider, where, orders);
        }
        public override ReturnList<T> GetUnionListByPage<T>(List<Expression<Func<TEntity, bool>>> whereList, ParaPage paraPage = null, List<ParaOrder<TEntity>> orders = null, bool hasTotal = true, bool isOnly = true)
        {
            var db = _yzContext;
            return db.GetUnionListByPage<TEntity, T>(base.AutoMapperConfigProvider, whereList, paraPage, orders, hasTotal, isOnly);
        }
        public override async Task<ReturnList<T>> GetUnionListByPageAsync<T>(List<Expression<Func<TEntity, bool>>> whereList, ParaPage paraPage = null, List<ParaOrder<TEntity>> orders = null, bool hasTotal = true, bool isOnly = true)
        {
            var db = _yzContext;
            return await db.GetUnionListByPageAsync<TEntity, T>(base.AutoMapperConfigProvider, whereList, paraPage, orders, hasTotal, isOnly);
        }
        public override int GetUnionTotal(List<Expression<Func<TEntity, bool>>> whereList)
        {
            var db = _yzContext;
            return db.GetUnionTotal(whereList);
        }
        public override async Task<int> GetUnionTotalAsync(List<Expression<Func<TEntity, bool>>> whereList)
        {
            var db = _yzContext;
            return await db.GetUnionTotalAsync(whereList);
        }
        public override bool IsExist(Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return db.IsExist(where);
        }
        public override async Task<bool> IsExistAsync(Expression<Func<TEntity, bool>> where = null)
        {
            var db = _yzContext;
            return await db.IsExistAsync(where);
        }
        #endregion
    }
}
